package com.tian.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年06月16日 10:26
 * 博客地址：<a href="http://woaijava.cc/">博客地址</a>
 * <p>
 * 项目线程池配置
 */
@Configuration
@EnableAsync
public class ThreadPoolConfig {

    private static final int CPU_CORE = Runtime.getRuntime().availableProcessors();

    /**
     * 线程池
     */
    @Bean
    public Executor executor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CPU_CORE * 2);
        executor.setMaxPoolSize(CPU_CORE * 2);
        executor.setQueueCapacity(50);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("CHARGE_STATION_EXECUTOR_THREAD_");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }
}